package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PResume;
import com.ruoyi.system.domain.bo.PResumeBo;
import com.ruoyi.system.domain.vo.PResumeVo;
import com.ruoyi.system.mapper.PResumeMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IPResumeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 咨询师个人简历Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-21
 */
@RequiredArgsConstructor
@Service
public class PResumeServiceImpl implements IPResumeService {

    private final PResumeMapper baseMapper;

    private final SysUserMapper userMapper;


    /**
     * 查询咨询师个人简历
     */
    @Override
    public PResumeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询咨询师个人简历列表
     */
    @Override
    public TableDataInfo<PResumeVo> queryPageList(PResumeBo bo, PageQuery pageQuery) {
        /*LambdaQueryWrapper<PResume> lqw = buildQueryWrapper(bo);
        Page<PResumeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);*/
        pageQuery.setPageNum((pageQuery.getPageNum() - 1) * pageQuery.getPageSize());
        List<PResumeVo> result = baseMapper.selectPageList(bo, pageQuery);

        return TableDataInfo.build(result);
    }

    /**
     * 查询咨询师个人简历列表
     */
    @Override
    public List<PResumeVo> queryList(PResumeBo bo) {
        LambdaQueryWrapper<PResume> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PResume> buildQueryWrapper(PResumeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PResume> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, PResume::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getDescInfo()), PResume::getDescInfo, bo.getDescInfo());
//        lqw.eq(StringUtils.isNotBlank(bo.getGoodDesc()), PResume::getGoodDesc, bo.getGoodDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkTime()), PResume::getWorkTime, bo.getWorkTime());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodTherapy()), PResume::getGoodTherapy, bo.getGoodTherapy());
        lqw.eq(StringUtils.isNotBlank(bo.getTrainingExperience()), PResume::getTrainingExperience, bo.getTrainingExperience());
        lqw.eq(StringUtils.isNotBlank(bo.getLevelInfo()), PResume::getLevelInfo, bo.getLevelInfo());
        lqw.eq(bo.getAgreement() != null, PResume::getAgreement, bo.getAgreement());
        lqw.eq(bo.getHelpPeople() != null, PResume::getHelpPeople, bo.getHelpPeople());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkType()), PResume::getWorkType, bo.getWorkType());
        lqw.eq(StringUtils.isNotBlank(bo.getPlatformGuarantee()), PResume::getPlatformGuarantee, bo.getPlatformGuarantee());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PResume::getStatus, bo.getStatus());
        lqw.eq(bo.getFreeTime() != null, PResume::getFreeTime, bo.getFreeTime());
        return lqw;
    }

    /**
     * 新增咨询师个人简历
     */
    @Override
    public Boolean insertByBo(PResumeBo bo) {
        PResume add = BeanUtil.toBean(bo, PResume.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改咨询师个人简历
     */
    @Override
    public Boolean updateByBo(PResumeBo bo) {
        PResume update = BeanUtil.toBean(bo, PResume.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PResume entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除咨询师个人简历
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 审核咨询师
     */
    @Override
    public Boolean approve(Long userId, String isFlag) {
        SysUser sysUser = userMapper.selectUserById(userId);

        sysUser.setIsFlag(isFlag);

        return userMapper.updateById(sysUser) > 0;
    }
}
